package com.gofunbuy.app.member;

import com.gofunbuy.R;
import com.gofunbuy.TabMemberGroup;
import com.gofunbuy.util.GoFunBuyUtils;
import com.gofunbuy.util.GoFunBuyUtils.GoFunBuyInterface;
import com.google.zxing.WriterException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class SubMemberTelCardActivity extends Activity implements
		OnClickListener, GoFunBuyInterface {
	private TextView tvBack,tvTitle;
	private ImageView ivTelCard;
	private Bitmap mCard;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_card_layout);
		initLayout();
	}

	private void initLayout() {
		tvTitle = (TextView) findViewById(R.id.topbar_title);
		tvTitle.setText(R.string.title_card);
		tvBack = (TextView) findViewById(R.id.topbar_back);
		tvBack.setOnClickListener(this);
		ivTelCard = (ImageView) findViewById(R.id.id_telcard);
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					if (GoFunBuyUtils.mUserID != null)
						mCard = GoFunBuyUtils
								.Create2DCode(GoFunBuyUtils.mUserID);
				} catch (WriterException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mHandler.sendEmptyMessage(MSG_GET_MESSAGE_STATE);
			}
		}).start();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.topbar_back:
			startActivity(TabMemberActivity.class);
			break;
		}
	}

	private void startActivity(Class<?> a) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(SubMemberTelCardActivity.this, a);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabMemberGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabMemberGroup.group.setContentView(view);
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case MSG_GET_MESSAGE_STATE:
				ivTelCard.setImageBitmap(mCard);
				break;

			}
		}

	};
}
